#include <iostream>
#include <string>
#include <vector>
#include <sw/redis++/redis++.h>
#include "utill.hpp"

using std::cout;
using std::endl;
using std::string;
using std::vector;

using sw::redis::Redis;

void test1(Redis &redis)
{
    redis.flushall();

    redis.zadd("key",
               {std::make_pair("吕布", 99),
                std::make_pair("赵云", 96)});

    vector<std::pair<string, double>> members{
        {"关羽", 95},
        {"张飞", 93}};
    redis.zadd("key", members.begin(), members.end());

    // zrange的查询分为两种：只查询member,不带score; 查询member和score
    // 两种查询通过插入迭代器指向的容器进行区分
    // 容器只包含string, 就是只查询member
    // 容器包含pair, 里面有string和double, 就是查询member和score
    vector<string> memberResults;
    auto it = std::back_inserter(memberResults);
    redis.zrange("key", 0, -1, it);
    PrintContainer(memberResults);

    vector<std::pair<string, double>> membersWithScore;
    auto it2 = std::back_inserter(membersWithScore);
    redis.zrange("key", 0, -1, it2);
    PrintPairContainer(membersWithScore);
}

void test2(Redis &redis)
{
    redis.flushall();

    redis.zadd("key", "张三", 90);
    redis.zadd("key", "李四", 93);
    redis.zadd("key", "王五", 94);
    redis.zadd("key", "赵六", 89);

    redis.zrem("key", "张三");

    long long card = redis.zcard("key");
    cout << "card: " << card << endl;

    auto score = redis.zscore("key", "王五");
    if (score)
        cout << "score: " << score.value() << endl;
    else
        cout << "score无效" << endl;

    auto rank = redis.zrank("key", "赵六");
    if (rank)
        cout << "rank: " << rank.value() << endl;
    else
        cout << "rank无效" << endl;
}

int main()
{
    Redis redis("tcp://127.0.0.1:6379");

    // test1(redis);
    test2(redis);

    return 0;
}